import { Vector2, Vector3 } from "three";
import CommonUtils from "./CommonUtils";
import DorsalRidgeUtils from "./DorsalRidgeUtils";
import { BIM } from "./BIM";

export default class TubeUtils {


    static getVIronPaths(s1: Vector3, e1: Vector3, typepath: any[], H: number): Vector3[][] {
        let dir: Vector3, normal: Vector3, bnor: Vector3, tnor: Vector3;

        let length: number, angle: number, type: number, th: number;

        let st: Vector3, et: Vector3, sb: Vector3, eb: Vector3;

        let up = new Vector3(0, 1, 0);

        let allPath: Vector3[][] = [];
        // 求第一个
        type = typepath[0][2];
        th = H;

        dir = e1.clone().sub(s1);

        normal = dir.clone().cross(up).normalize();

        bnor = dir.clone().applyAxisAngle(normal, -Math.PI / 2);
        tnor = bnor.clone().negate();

        bnor.setLength(th / 2);
        tnor.setLength(th / 2);

        st = s1.clone().add(tnor);
        et = e1.clone().add(tnor);
        sb = s1.clone().add(bnor);
        eb = e1.clone().add(bnor);

        let path: Vector3[] = [st, sb, eb, et, st];

        allPath.push(path);

        for (let i = 1; i < typepath.length; i++) {
            length = typepath[i][0];
            angle = typepath[i][1];
            type = typepath[i][2];

            // 管厚度
            th = H;

            path = DorsalRidgeUtils.getVIronPath(allPath[allPath.length - 1], length, angle / 180 * Math.PI, th, normal, i % 2 === 0);

            allPath.push(path);
        }
        return allPath;
    }

    static getTubeJGVI(count: number, vertices: any[], indices: any[], facevec: any[],
        jgW: number, jgH: number, tphd: number, L1: number, L2: number, xjgdjxg_dis: number,
        vdir: Vector3, hdir: Vector3, af: Vector3, bf: Vector3): any[] {

        let p0: Vector3 = af.clone().add(hdir.setLength(xjgdjxg_dis)).add(vdir.setLength(L2));
        let p1: Vector3 = p0.clone().sub(vdir.setLength(jgH));
        let p2: Vector3 = p1.clone().add(hdir.setLength(L1));
        let p4: Vector3 = p0.clone().add(hdir.setLength(jgW));
        let p3: Vector3 = p4.clone().sub(vdir.setLength(L2));
        // 厚度方向
        let abfdir = af.clone().sub(bf);

        let tp0: Vector3 = p0.clone().add(abfdir.setLength(tphd));
        let tp1: Vector3 = p1.clone().add(abfdir.setLength(tphd));
        let tp2: Vector3 = p2.clone().add(abfdir.setLength(tphd));
        let tp3: Vector3 = p3.clone().add(abfdir.setLength(tphd));
        let tp4: Vector3 = p4.clone().add(abfdir.setLength(tphd));

        let frontface = [p0, p1, p2, p3, p4, p0];
        let backface = [tp0, tp1, tp2, tp3, tp4, tp0];

        let path = [
            new Vector2(xjgdjxg_dis, L2),
            new Vector2(xjgdjxg_dis + jgW, L2),
            new Vector2(xjgdjxg_dis + jgW, 0),
            new Vector2(xjgdjxg_dis + L1, -jgH + L2),
            new Vector2(xjgdjxg_dis, -jgH + L2)
        ];

        count = CommonUtils.getFaceByFunction(count, vertices, indices, facevec, frontface, path, null, null, (a: number, b: number) => {

            let result = af.clone().add(hdir.clone().setLength(a)).add(vdir.clone().setLength(b));
            return [result.x, result.y, result.z];
        });

        if (!BIM.simpleMold) {
            count = CommonUtils.getFaceByFunction(count, vertices, indices, facevec, backface, path, null, null, (a: number, b: number) => {

                let result = af.clone().add(hdir.clone().setLength(a)).add(vdir.clone().setLength(b)).add(abfdir.setLength(tphd));
                return [result.x, result.y, result.z];
            });


            let sides = CommonUtils.getSideVIF(backface, frontface, count);
            vertices.push(...sides[0]);
            indices.push(...sides[1]);
            facevec.push(...sides[2]);
            count = sides[3];
        }



        let bp0: Vector3 = bf.clone().add(hdir.setLength(xjgdjxg_dis)).add(vdir.setLength(L2));
        let bp1: Vector3 = bp0.clone().sub(vdir.setLength(jgH));
        let bp2: Vector3 = bp1.clone().add(hdir.setLength(L1));
        let bp4: Vector3 = bp0.clone().add(hdir.setLength(jgW));
        let bp3: Vector3 = bp4.clone().sub(vdir.setLength(L2));

        let tbp0: Vector3 = bp0.clone().sub(abfdir.setLength(tphd));
        let tbp1: Vector3 = bp1.clone().sub(abfdir.setLength(tphd));
        let tbp2: Vector3 = bp2.clone().sub(abfdir.setLength(tphd));
        let tbp3: Vector3 = bp3.clone().sub(abfdir.setLength(tphd));
        let tbp4: Vector3 = bp4.clone().sub(abfdir.setLength(tphd));


        frontface = [bp0, bp1, bp2, bp3, bp4, bp0];
        backface = [tbp0, tbp1, tbp2, tbp3, tbp4, tbp0];

        count = CommonUtils.getFaceByFunction(count, vertices, indices, facevec, frontface, path, null, null, (a: number, b: number) => {

            let result = bf.clone().add(hdir.clone().setLength(a)).add(vdir.clone().setLength(b));
            return [result.x, result.y, result.z];
        });

        if (!BIM.simpleMold) {
            count = CommonUtils.getFaceByFunction(count, vertices, indices, facevec, backface, path, null, null, (a: number, b: number) => {

                let result = bf.clone().add(hdir.clone().setLength(a)).add(vdir.clone().setLength(b)).sub(abfdir.setLength(tphd));
                return [result.x, result.y, result.z];
            });

            let sides = CommonUtils.getSideVIF(backface, frontface, count);
            vertices.push(...sides[0]);
            indices.push(...sides[1]);
            facevec.push(...sides[2]);
            count = sides[3];
        }

        let center = bp0.clone().add(bp1).multiplyScalar(0.5);
        center.setY(0);
        // let tcenter = tbp0.clone().add(tbp1).multiplyScalar(0.5);
        // let center = bcenter.add(tcenter).multiplyScalar(0.5);
        return [count, center, new Vector3(-1, 0, -1)];
    }

    /** 水平方通 */
    static getTubeHorizontalVI(count: number, vertices: number[], indices: number[], facevec: Vector3[][],
        s1: Vector3, e1: Vector3, typepath: number[][], W: number, H: number,
        p_starttype: number, p_endtype: number, L1: number, L2: number, xjgdjxg_dis: number,
        xjcd: number, xjkd: number, tphd: number, jgW: number, jgH: number, p_partdirection: number,
        data: any, isVertical: boolean) {

        let blpaths = DorsalRidgeUtils.getDorsalridgePath(s1, e1, typepath, W, true);

        let sides: any, a: Vector3, b: Vector3, c: Vector3, d: Vector3;
        let esdir: Vector3, sedir: Vector3;
        let normal: Vector3;

        let frontface: Vector3[], backface: Vector3[];
        let xj1: Vector3, xj2: Vector3, xj3: Vector3;

        let p_startextend = Number(data.p_startextend);
        let p_endextend = Number(data.p_endextend);

        let startDir: Vector3, endDir: Vector3;
        let mwdir: Vector3, mldir: Vector3, mc: Vector3;
        let nwdir: Vector3, nldir: Vector3, nc: Vector3;
        let hdir: Vector3, vdir: Vector3;

        data.zhong = {};
        data.duan = [];
        data.jgkong = [];
        let jgData: any[];
        for (let idx = 0; idx < blpaths.length; idx++) {

            a = blpaths[idx][0][0];
            b = blpaths[idx][0][1];
            c = blpaths[idx][0][2];
            d = blpaths[idx][0][3];

            normal = blpaths[idx][1];

            if (idx === 0) {
                // 处理延申
                startDir = c.clone().sub(b).setLength(p_startextend);
                c.add(startDir);
                d.add(startDir);
            }

            if (idx === blpaths.length - 1) {
                // 处理延申
                endDir = c.clone().sub(b).setLength(p_endextend);
                c.add(endDir);
                d.add(endDir);
            }

            if (idx === 0) {
                if (p_starttype === 1) {
                    // 斜角
                    if (p_partdirection === 0) {
                        esdir = d.clone().sub(a).setLength(xjcd)
                        xj1 = a.clone().add(esdir);
                        xj2 = b.clone().add(esdir);
                        xj3 = a.clone().add(normal.clone().setLength(W - xjkd));

                        frontface = [
                            new Vector3(xj1.x, H / 2, xj1.z),
                            new Vector3(a.x, H / 2, a.z),
                            new Vector3(xj3.x, H / 2, xj3.z),
                            new Vector3(xj2.x, H / 2, xj2.z),

                            new Vector3(xj1.x, H / 2, xj1.z)
                        ]
                        backface = [
                            new Vector3(xj1.x, -H / 2, xj1.z),
                            new Vector3(a.x, -H / 2, a.z),
                            new Vector3(xj3.x, -H / 2, xj3.z),
                            new Vector3(xj2.x, -H / 2, xj2.z),

                            new Vector3(xj1.x, -H / 2, xj1.z)
                        ];

                        a = xj1.clone();
                        b = xj2.clone();
                    }
                    else {
                        esdir = c.clone().sub(b).setLength(xjcd)
                        xj1 = b.clone().add(esdir);
                        xj2 = a.clone().add(esdir);
                        xj3 = b.clone().sub(normal.clone().setLength(W - xjkd));

                        frontface = [
                            new Vector3(b.x, H / 2, b.z),
                            new Vector3(xj1.x, H / 2, xj1.z),
                            new Vector3(xj2.x, H / 2, xj2.z),
                            new Vector3(xj3.x, H / 2, xj3.z),

                            new Vector3(b.x, H / 2, b.z)
                        ]
                        backface = [
                            new Vector3(b.x, -H / 2, b.z),
                            new Vector3(xj1.x, -H / 2, xj1.z),
                            new Vector3(xj2.x, -H / 2, xj2.z),
                            new Vector3(xj3.x, -H / 2, xj3.z),

                            new Vector3(b.x, -H / 2, b.z)
                        ];

                        a = xj2.clone();
                        b = xj1.clone();
                    }


                    sides = CommonUtils.getVIF(backface, frontface, count);
                    vertices.push(...sides[0]);
                    indices.push(...sides[1]);
                    facevec.push(...sides[2]);
                    count = sides[3];

                }
                else if (p_starttype === 2) {
                    // 角钢
                    hdir = d.clone().sub(a);
                    vdir = a.clone().sub(b);
                    if (p_partdirection === 0) {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone(), hdir.clone(), new Vector3(b.x, H / 2, b.z), new Vector3(b.x, -H / 2, b.z));
                        count = jgData[0];
                        data.jgkong.push(0, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);
                    }
                    else {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone().negate(), hdir.clone(), new Vector3(a.x, H / 2, a.z), new Vector3(a.x, -H / 2, a.z));
                        count = jgData[0];
                        data.jgkong.push(0, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);
                    }
                }

                // 头
                let snr = a.clone().sub(b).normalize();
                let ser = c.clone().sub(b).normalize();

                data.bltou = [b.x, 0, b.z, snr.x, snr.y, snr.z, ser.x, ser.y, ser.z, 0, -1, 0];

                let tipsData = DorsalRidgeUtils.addHeadTips(count, W, H, a, d, normal.clone());
                vertices.push(...tipsData[0]);
                indices.push(...tipsData[1]);
                count = tipsData[2];
            }

            if (idx === blpaths.length - 1) {

                if (p_endtype === 1) {
                    // 斜角

                    if (p_partdirection === 0) {
                        sedir = a.clone().sub(d).setLength(xjcd);
                        xj1 = d.clone().add(sedir);
                        xj2 = c.clone().add(sedir);
                        xj3 = d.clone().add(normal.clone().setLength(W - xjkd));

                        frontface = [
                            new Vector3(xj1.x, H / 2, xj1.z),
                            new Vector3(xj2.x, H / 2, xj2.z),
                            new Vector3(xj3.x, H / 2, xj3.z),
                            new Vector3(d.x, H / 2, d.z),

                            new Vector3(xj1.x, H / 2, xj1.z)

                        ]
                        backface = [
                            new Vector3(xj1.x, -H / 2, xj1.z),
                            new Vector3(xj2.x, -H / 2, xj2.z),
                            new Vector3(xj3.x, -H / 2, xj3.z),
                            new Vector3(d.x, -H / 2, d.z),

                            new Vector3(xj1.x, -H / 2, xj1.z)
                        ];

                        d = xj1.clone();
                        c = xj2.clone();
                    }
                    else {
                        sedir = b.clone().sub(c).setLength(xjcd);
                        xj1 = d.clone().add(sedir);
                        xj2 = c.clone().add(sedir);
                        xj3 = c.clone().sub(normal.clone().setLength(W - xjkd));

                        frontface = [
                            new Vector3(xj1.x, H / 2, xj1.z),
                            new Vector3(xj2.x, H / 2, xj2.z),
                            new Vector3(c.x, H / 2, c.z),
                            new Vector3(xj3.x, H / 2, xj3.z),

                            new Vector3(xj1.x, H / 2, xj1.z)
                        ]
                        backface = [
                            new Vector3(xj1.x, -H / 2, xj1.z),
                            new Vector3(xj2.x, -H / 2, xj2.z),
                            new Vector3(c.x, -H / 2, c.z),
                            new Vector3(xj3.x, -H / 2, xj3.z),

                            new Vector3(xj1.x, -H / 2, xj1.z)
                        ];

                        d = xj1.clone();
                        c = xj2.clone();
                    }
                    sides = CommonUtils.getVIF(backface, frontface, count);
                    vertices.push(...sides[0]);
                    indices.push(...sides[1]);
                    facevec.push(...sides[2]);
                    count = sides[3];
                }
                else if (p_endtype === 2) {

                    hdir = d.clone().sub(a);
                    vdir = a.clone().sub(b);
                    if (p_partdirection === 0) {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone(), hdir.clone().negate(), new Vector3(c.x, H / 2, c.z), new Vector3(c.x, -H / 2, c.z));
                        count = jgData[0];
                        data.jgkong.push(1, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);
                    }
                    else {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone().negate(), hdir.clone().negate(), new Vector3(d.x, H / 2, d.z), new Vector3(d.x, -H / 2, d.z));
                        count = jgData[0];
                        data.jgkong.push(1, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);
                    }
                }

                // 尾
                let ner = d.clone().sub(c).normalize();
                let eer = b.clone().sub(c).normalize();
                data.blwei = [c.x, 0, c.z, ner.x, ner.y, ner.z, eer.x, eer.y, eer.z, 0, -1, 0];
            }

            let halfH = H / 2;
            data.duan.push(
                a.x, halfH, a.z,
                b.x, halfH, b.z,
                c.x, halfH, c.z,
                d.x, halfH, d.z,

                a.x, -halfH, a.z,
                b.x, -halfH, b.z,
                c.x, -halfH, c.z,
                d.x, -halfH, d.z
            )

            // 拉伸点
            mwdir = a.clone().add(b).multiplyScalar(0.5);
            mldir = c.clone().add(d).multiplyScalar(0.5);
            mc = mwdir.clone().add(mldir).multiplyScalar(0.5);

            nwdir = a.clone().add(d).multiplyScalar(0.5);
            nldir = b.clone().add(c).multiplyScalar(0.5);
            nc = nwdir.clone().add(nldir).multiplyScalar(0.5);

            if (blpaths.length === 1) {
                // 给头尾
                data.zhong["head"] = [
                    {
                        idx: idx,
                        tips: 0,
                        pointer: [mwdir.x, halfH, mwdir.z],
                        center: [mc.x, halfH, mc.z]
                    }
                ]

                data.zhong["tail"] = [
                    {
                        idx: idx,
                        tips: 0,
                        pointer: [mldir.x, halfH, mldir.z],
                        center: [mc.x, halfH, mc.z]
                    }
                ]
            }
            else {

                if (idx == 0) {
                    // 头
                    data.zhong["head"] = [
                        {
                            idx: idx,
                            tips: 0,
                            pointer: [mwdir.x, halfH, mwdir.z],
                            center: [mc.x, halfH, mc.z]
                        },
                        {
                            idx: idx,
                            tips: 1,
                            pointer: [nwdir.x, halfH, nwdir.z],
                            center: [nc.x, halfH, nc.z]
                        },
                        {
                            idx: idx,
                            tips: 2,
                            pointer: [nldir.x, halfH, nldir.z],
                            center: [nc.x, halfH, nc.z]
                        }
                    ]
                }
                else if (idx == blpaths.length - 1) {
                    // 尾
                    data.zhong["tail"] = [
                        {
                            idx: idx,
                            tips: 0,
                            pointer: [mldir.x, halfH, mldir.z],
                            center: [mc.x, halfH, mc.z]
                        },
                        {
                            idx: idx,
                            tips: 1,
                            pointer: [nwdir.x, halfH, nwdir.z],
                            center: [nc.x, halfH, nc.z]
                        },
                        {
                            idx: idx,
                            tips: 2,
                            pointer: [nldir.x, halfH, nldir.z],
                            center: [nc.x, halfH, nc.z]
                        }
                    ]
                }
                else {
                    if (isVertical) {

                        // 中间
                        data.zhong[idx] = [
                            {
                                idx: idx,
                                tips: 0,
                                pointer: [nwdir.x, halfH, nwdir.z],
                                center: [nc.x, halfH, nc.z]
                            },
                            {
                                idx: idx,
                                tips: 1,
                                pointer: [nldir.x, halfH, nldir.z],
                                center: [nc.x, halfH, nc.z]
                            }
                        ]
                    }
                }
            }

            // 上背楞
            frontface = [
                new Vector3(a.x, -halfH, a.z),
                new Vector3(b.x, -halfH, b.z),
                new Vector3(b.x, halfH, b.z),
                new Vector3(a.x, halfH, a.z)
            ]

            backface = [
                new Vector3(d.x, -halfH, d.z),
                new Vector3(c.x, -halfH, c.z),
                new Vector3(c.x, halfH, c.z),
                new Vector3(d.x, halfH, d.z)
            ]

            // 侧面
            sides = CommonUtils.getVIF([...backface, backface[0]], [...frontface, frontface[0]], count);
            vertices.push(...sides[0]);
            indices.push(...sides[1]);
            facevec.push(...sides[2]);
            count = sides[3];

        }
    }

    /**  竖向多边方通 */
    static getTubeVerticalVI(count: number, vertices: number[], indices: number[], facevec: Vector3[][],
        s1: Vector3, e1: Vector3, typepath: number[][], W: number, H: number,
        p_starttype: number, p_endtype: number, L1: number, L2: number, xjgdjxg_dis: number,
        xjcd: number, xjkd: number, tphd: number, jgW: number, jgH: number, p_partdirection: number,
        data: ALParams, isVertical: boolean) {

        let up = new Vector3(0, 1, 0);
        let se = e1.clone().sub(s1);
        let normal = se.clone().cross(up).setLength(W);
        let paths = TubeUtils.getVIronPaths(s1, e1, typepath, H);

        let a: Vector3, b: Vector3, c: Vector3, d: Vector3;
        let ab: Vector3, bb: Vector3, cb: Vector3, db: Vector3;
        let af: Vector3, bf: Vector3, cf: Vector3, df: Vector3;

        let esdir: Vector3, sedir: Vector3;
        let xj1: Vector3, xj2: Vector3, xj3: Vector3, xj4: Vector3, xj5: Vector3, xj6: Vector3;
        let frontface: Vector3[], backface: Vector3[];

        let hdir: Vector3, vdir: Vector3;

        let sides: any[];

        let p_startextend = Number(data.p_startextend);
        let p_endextend = Number(data.p_endextend);
        let startDir: Vector3, endDir: Vector3;

        let mwdir: Vector3, mldir: Vector3, mc: Vector3;
        let nwdir: Vector3, nldir: Vector3, nc: Vector3;


        data.zhong = {};
        data.duan = [];
        data.jgkong = [];
        let jgData: any[];
        for (let idx = 0; idx < paths.length; idx++) {
            a = paths[idx][0];
            b = paths[idx][1];
            c = paths[idx][2];
            d = paths[idx][3];

            if (idx === 0) {
                // 处理延申
                startDir = d.clone().sub(a).setLength(p_startextend);
                c.add(startDir);
                d.add(startDir);

            }

            if (idx === paths.length - 1) {
                // 处理延申
                endDir = d.clone().sub(a).setLength(p_endextend);
                c.add(endDir);
                d.add(endDir);
            }

            // 轮廓点
            ab = a.clone();
            bb = b.clone();
            cb = c.clone();
            db = d.clone();

            af = a.clone().add(normal);
            bf = b.clone().add(normal);
            cf = c.clone().add(normal);
            df = d.clone().add(normal);

            // 头
            if (idx === 0) {
                if (p_starttype === 1) {

                    // 斜角
                    esdir = d.clone().sub(a).setLength(xjcd);

                    if (p_partdirection === 0) {
                        xj1 = ab.clone().add(esdir);
                        xj2 = bb.clone().add(esdir);
                        xj3 = af.clone().add(esdir);
                        xj4 = bf.clone().add(esdir);
                        xj5 = ab.clone().add(normal.clone().setLength(W - xjkd));
                        xj6 = bb.clone().add(normal.clone().setLength(W - xjkd));

                        frontface = [xj1, xj3, xj5, ab.clone(), xj1];
                        backface = [xj2, xj4, xj6, bb.clone(), xj2];

                        ab = xj1.clone();
                        bb = xj2.clone();
                        af = xj3.clone();
                        bf = xj4.clone();
                    }
                    else {
                        xj1 = ab.clone().add(esdir);
                        xj2 = bb.clone().add(esdir);
                        xj3 = af.clone().add(esdir);
                        xj4 = bf.clone().add(esdir);
                        xj5 = af.clone().sub(normal.clone().setLength(W - xjkd));
                        xj6 = bf.clone().sub(normal.clone().setLength(W - xjkd));

                        frontface = [xj1, xj3, af.clone(), xj5, xj1];
                        backface = [xj2, xj4, bf.clone(), xj6, xj2];

                        ab = xj1.clone();
                        bb = xj2.clone();
                        af = xj3.clone();
                        bf = xj4.clone();
                    }

                    sides = CommonUtils.getVIF(backface, frontface, count);
                    vertices.push(...sides[0]);
                    indices.push(...sides[1]);
                    facevec.push(...sides[2]);
                    count = sides[3];

                }
                else if (p_starttype === 2) {

                    // 角钢
                    hdir = df.clone().sub(af);
                    vdir = ab.clone().sub(af);
                    if (p_partdirection === 0) {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone(), hdir.clone(), af, bf);
                        count = jgData[0];
                        data.jgkong.push(0, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);
                    }
                    else {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone().negate(), hdir.clone(), ab, bb);
                        count = jgData[0];
                        data.jgkong.push(0, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);

                    }
                }


                let tounor = ab.clone().sub(af).normalize();
                let toudir = cf.clone().sub(af).normalize();
                let touDown = bf.clone().sub(af).normalize();
                let tou = af.clone().add(bf).multiplyScalar(0.5);
                data.bltou = [tou.x, tou.y, tou.z, tounor.x, tounor.y, tounor.z, toudir.x, toudir.y, toudir.z, touDown.x, touDown.y, touDown.z];


                let tipsData = DorsalRidgeUtils.addHeadTips(count, W, H, ab, db, normal.clone());
                vertices.push(...tipsData[0]);
                indices.push(...tipsData[1]);
                count = tipsData[2];
            }

            // 尾
            if (idx === paths.length - 1) {
                if (p_endtype === 1) {
                    // 斜角
                    sedir = a.clone().sub(d).setLength(xjcd);
                    if (p_partdirection === 0) {
                        xj1 = db.clone().add(sedir);
                        xj2 = cb.clone().add(sedir);
                        xj3 = df.clone().add(sedir);
                        xj4 = cf.clone().add(sedir);

                        xj5 = db.clone().add(normal.clone().setLength(W - xjkd));
                        xj6 = cb.clone().add(normal.clone().setLength(W - xjkd));

                        frontface = [xj1, xj3, xj5, db.clone(), xj1];
                        backface = [xj2, xj4, xj6, cb.clone(), xj2];

                        db = xj1.clone();
                        cb = xj2.clone();
                        df = xj3.clone();
                        cf = xj4.clone();
                    }
                    else {
                        xj1 = db.clone().add(sedir);
                        xj2 = cb.clone().add(sedir);
                        xj3 = df.clone().add(sedir);
                        xj4 = cf.clone().add(sedir);

                        xj5 = df.clone().sub(normal.clone().setLength(W - xjkd));
                        xj6 = cf.clone().sub(normal.clone().setLength(W - xjkd));

                        frontface = [xj1, xj3, df.clone(), xj5, xj1];
                        backface = [xj2, xj4, cf.clone(), xj6, xj2];

                        db = xj1.clone();
                        cb = xj2.clone();
                        df = xj3.clone();
                        cf = xj4.clone();
                    }


                    sides = CommonUtils.getVIF(backface, frontface, count);
                    vertices.push(...sides[0]);
                    indices.push(...sides[1]);
                    facevec.push(...sides[2]);
                    count = sides[3];
                }
                else if (p_endtype === 2) {
                    // 角钢
                    hdir = df.clone().sub(af);
                    vdir = ab.clone().sub(af);
                    if (p_partdirection === 0) {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone(), hdir.clone().negate(), df, cf);
                        count = jgData[0];
                        data.jgkong.push(1, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);
                    }
                    else {
                        jgData = TubeUtils.getTubeJGVI(count, vertices, indices, facevec, jgW, jgH, tphd, L1, L2, xjgdjxg_dis,
                            vdir.clone().negate(), hdir.clone().negate(), db, cb);
                        count = jgData[0];
                        data.jgkong.push(1, jgData[1].x, jgData[1].y, jgData[1].z,
                            jgData[2].x, jgData[2].y, jgData[2].z);

                    }
                }

                let weinor = db.clone().sub(df).normalize();
                let weidir = af.clone().sub(df).normalize();
                let weiDown = cf.clone().sub(df).normalize();
                let wei = df.clone().add(cf).multiplyScalar(0.5);
                data.blwei = [wei.x, wei.y, wei.z, weinor.x, weinor.y, weinor.z, weidir.x, weidir.y, weidir.z, weiDown.x, weiDown.y, weiDown.z];

            }

            data.duan.push(
                ab.x, ab.y, ab.z,
                bb.x, bb.y, bb.z,
                cb.x, cb.y, cb.z,
                db.x, db.y, db.z,
                af.x, af.y, af.z,
                bf.x, bf.y, bf.z,
                cf.x, cf.y, cf.z,
                df.x, df.y, df.z
            )

            // 拉伸点
            mwdir = ab.clone().add(af).multiplyScalar(0.5);
            mldir = db.clone().add(df).multiplyScalar(0.5);
            mc = mwdir.clone().add(mldir).multiplyScalar(0.5);

            nwdir = af.clone().add(df).multiplyScalar(0.5);
            nldir = bf.clone().add(cf).multiplyScalar(0.5);
            nc = nwdir.clone().add(nldir).multiplyScalar(0.5);

            if (paths.length === 1) {
                // 给头尾
                data.zhong["head"] = [
                    {
                        idx: idx,
                        tips: 0,
                        pointer: [mwdir.x, mwdir.y, mwdir.z],
                        center: [mc.x, mc.y, mc.z]
                    }
                ]

                data.zhong["tail"] = [
                    {
                        idx: idx,
                        tips: 0,
                        pointer: [mldir.x, mldir.y, mldir.z],
                        center: [mc.x, mc.y, mc.z]
                    }
                ]
            }
            else {

                if (idx == 0) {
                    // 头
                    data.zhong["head"] = [
                        {
                            idx: idx,
                            tips: 0,
                            pointer: [mwdir.x, mwdir.y, mwdir.z],
                            center: [mc.x, mc.y, mc.z]
                        },
                        {
                            idx: idx,
                            tips: 1,
                            pointer: [nwdir.x, nwdir.y, nwdir.z],
                            center: [nc.x, nc.y, nc.z]
                        },
                        {
                            idx: idx,
                            tips: 2,
                            pointer: [nldir.x, nldir.y, nldir.z],
                            center: [nc.x, nc.y, nc.z]
                        }
                    ]
                }
                else if (idx == paths.length - 1) {
                    // 尾
                    data.zhong["tail"] = [
                        {
                            idx: idx,
                            tips: 0,
                            pointer: [mldir.x, mldir.y, mldir.z],
                            center: [mc.x, mc.y, mc.z]
                        },
                        {
                            idx: idx,
                            tips: 1,
                            pointer: [nwdir.x, nwdir.y, nwdir.z],
                            center: [nc.x, nc.y, nc.z]
                        },
                        {
                            idx: idx,
                            tips: 2,
                            pointer: [nldir.x, nldir.y, nldir.z],
                            center: [nc.x, nc.y, nc.z]
                        }
                    ]
                }
                else {
                    if (isVertical) {
                        // 中间
                        data.zhong[idx] = [
                            {
                                idx: idx,
                                tips: 0,
                                pointer: [nwdir.x, nwdir.y, nwdir.z],
                                center: [nc.x, nc.y, nc.z]
                            },
                            {
                                idx: idx,
                                tips: 1,
                                pointer: [nldir.x, nldir.y, nldir.z],
                                center: [nc.x, nc.y, nc.z]
                            }
                        ]
                    }
                }
            }


            frontface = [ab, bb, bf, af];
            backface = [db, cb, cf, df];

            facevec.push([ab, bb, bf, af]);
            facevec.push([db, cb, cf, df]);

            // 侧面
            sides = CommonUtils.getVIF([...backface, backface[0]], [...frontface, frontface[0]], count);
            vertices.push(...sides[0]);
            indices.push(...sides[1]);
            facevec.push(...sides[2]);
            count = sides[3];

        }
    }

    /**
    * 方通
    * @param data 
    * @returns 
    */
    static getTubeVI(data: ALParams): any[] {

        // 计数
        let count: number = 0;
        // 数据
        let vertices: any[] = [];
        let indices: any[] = [];
        let facevec: any[] = [];

        // 长宽高
        let L: number = Number(data.L);
        let W: number = Number(data.W);
        let H: number = Number(data.H);

        // 斜角长度
        let xjcd = Number(data.xjcd);
        // 斜角宽度
        let xjkd = Number(data.xjkd);

        // 铁皮厚度
        let tphd = Number(data.tphd);


        // 斜角钢端矩形管边距
        let xjgdjxg_dis = Number(data.xjgdjxg_dis);

        // 角钢宽
        let jgW = Number(data.jgW);
        // 角钢高
        let jgH = Number(data.jgH);

        // 长度12
        let L1: number = Number(data.L1);
        let L2: number = Number(data.L2);

        // 延申
        let p_startextend = Number(data.p_startextend);
        let p_endextend = Number(data.p_endextend);

        // 长度及角度
        if (!data.zhcs || data.zhcs === "") {
            data.zhcs = '(' + L + ',' + 0 + ',' + 0 + ')';
        }

        if (!data.p_sepoint || data.p_sepoint.length != 3) {
            if (!isNaN(L)) {
                data.p_sepoint = [L, 0, 0];
            }
        }

        let p_starttype = data.p_starttype;
        let p_endtype = data.p_endtype;
        let p_partdirection = data.p_partdirection;


        // 检测类型
        let typepath = DorsalRidgeUtils.getLenagData(data.zhcs);

        let isVertical: boolean = DorsalRidgeUtils.checkDRVertical(typepath);
        // 过滤掉非法输入
        let str: string = '';
        for (let i = 0; i < typepath.length; i++) {
            str += '(' + typepath[i][0] + ',' + typepath[i][1] + ',' + typepath[i][2] + ')'
        }
        data.zhcs = str;

        // 
        if (!data.p_sepoint || data.p_sepoint.length != 3) {
            data.p_sepoint = [typepath[0][0], 0, 0];
        }

        let p_sepoint = data.p_sepoint;
        // 起始端点
        let s1 = new Vector3(0, 0, 0);
        let e1 = new Vector3(p_sepoint[0], p_sepoint[1], p_sepoint[2]);

        // 第一段长
        let lenth = typepath[0][0];

        let sedis = s1.distanceTo(e1);
        if (Math.abs(sedis - lenth) > 0.5) {
            // 认为两个不相等
            let sedir = e1.clone().sub(s1);
            sedir.setLength(lenth);
            e1 = sedir;
        }

        let type = typepath[0][2];
        if (type != 0) {
            // 单管
            TubeUtils.getTubeVerticalVI(count, vertices, indices, facevec, s1, e1, typepath, W, H,
                p_starttype, p_endtype, L1, L2, xjgdjxg_dis, xjcd, xjkd, tphd, jgW, jgH, p_partdirection,
                data, isVertical);
        }
        else {
            // 默认水平
            TubeUtils.getTubeHorizontalVI(count, vertices, indices, facevec, s1, e1, typepath, W, H,
                p_starttype, p_endtype, L1, L2, xjgdjxg_dis, xjcd, xjkd, tphd, jgW, jgH,
                p_partdirection, data, isVertical);
        }

        str = '';
        for (let i = 0; i < typepath.length; i++) {
            if (i === 0) {
                typepath[i][0] += p_startextend;
            }
            if (i === typepath.length - 1) {
                typepath[i][0] += p_endextend;
            }

            str += '(' + typepath[i][0] + ',' + typepath[i][1] + ',' + typepath[i][2] + ')'
        }
        data.zhcs = str;

        return [vertices, indices, facevec];
    }

}